package com.example.sys.wx.service;

import com.example.security.service.SysTokenService;
import com.example.sys.org.po.SysUser;
import com.example.sys.org.service.SysUserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class WechatMpServiceImpl implements WechatMpService {

    @Resource
    private SysUserService sysUserService;

    @Resource
    private SysTokenService sysTokenService;

    /****
     *
     * 通过返回的openId创建用户并登录返回token
     * @param openId
     * @return
     * @throws Exception
     */
    @Override
    public String login(String openId) throws Exception {
        SysUser sysUser = this.sysUserService.loadUserByOpenId(openId);
        if (sysUser == null) {
            sysUser = this.sysUserService.registerWxUser(openId);
        }
        return sysTokenService.createToken(sysUser.getId());
    }
}
